/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.mapdemo;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnCameraChangeListener;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.TextView;
import android.widget.Toast;

/**
 * This shows how to listen to some {@link GoogleMap} events.
 */
public class EventsDemoActivity extends FragmentActivity
        implements OnMapClickListener, OnMapLongClickListener, OnCameraChangeListener {

    private GoogleMap mMap;
    private TextView mTapTextView;
    private TextView mCameraTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.events_demo);

        mTapTextView = (TextView) findViewById(R.id.tap_text);
        mCameraTextView = (TextView) findViewById(R.id.camera_text);

        setUpMapIfNeeded();
    }

    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }

    private void setUpMapIfNeeded() {
        if (mMap == null) {
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            if (mMap != null) {
                setUpMap();
            }
        }
    }
    private static final LatLng NEWARK = new LatLng(22.6090435, 120.31573995);
    private void setUpMap() {
    	mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(NEWARK, 15));
    	
        mMap.setOnMapClickListener(this);
        mMap.setOnMapLongClickListener(this);
        mMap.setOnCameraChangeListener(this);
        
        // Mark 地標資訊
        MarkerOptions markerOptions = new MarkerOptions().position(new LatLng(22.6090435, 120.31573995)).title("目前位置") ;

        
        // 變更Mark icon 圖案
        // 1. 使用預設圖片 
//      markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));
        // 2. 使用bitmap
        Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.arrow) ;
        BitmapDescriptorFactory.fromBitmap(bitmap);
        markerOptions.icon(BitmapDescriptorFactory.fromBitmap(bitmap));
        
        // 地標說明的描述 
        markerOptions.snippet("地標說明內容");

        mMap.addMarker(markerOptions);
        mMap.addMarker(new MarkerOptions().position(new LatLng(22.7957717, 120.3046276)).title("目的地"));
        
        LatLng origin = new LatLng(22.6090435, 120.31573995) ;
        LatLng dest = new LatLng(22.7957717, 120.3372866) ;
        String strUrl = getDirectionsUrl(origin,dest);
        Toast.makeText(this, " strUrl = "+strUrl, Toast.LENGTH_SHORT).show();
        
        PolylineOptions lineOptions = new PolylineOptions();
        LatLng position = new LatLng(22.6090435, 120.31573995);
        LatLng position2 = new LatLng(22.7957717, 120.3046276);
        lineOptions.add(position) ;
        lineOptions.add(position2) ;
        lineOptions.width(10);
        lineOptions.color(Color.BLUE);
        mMap.addPolyline(lineOptions);
    }

    private String getDirectionsUrl(LatLng origin, LatLng dest) {

    	  // Origin of route
    	  String str_origin = "origin=" + origin.latitude + ","
    	    + origin.longitude;

    	  // Destination of route
    	  String str_dest = "destination=" + dest.latitude + "," + dest.longitude;

    	  // Sensor enabled
    	  String sensor = "sensor=false";

    	  // Building the parameters to the web service
    	  String parameters = str_origin + "&" + str_dest + "&" + sensor;

    	  // Output format
    	  String output = "json";

    	  // Building the url to the web service
    	  String url = "https://maps.googleapis.com/maps/api/directions/"
    	    + output + "?" + parameters;

    	  return url;
    	 }
    
    @Override
    public void onMapClick(LatLng point) {
        mTapTextView.setText("tapped, point=" + point);
    }

    @Override
    public void onMapLongClick(LatLng point) {
        mTapTextView.setText("long pressed, point=" + point);
    }

    @Override
    public void onCameraChange(final CameraPosition position) {
        mCameraTextView.setText(position.toString());
    }
}
